var student = require("./user_model").student;
var group = require("../hackson_model").group

var login = (req, res, tch) => {
    var number = req.body.stNumber
    var password = req.body.password

    student.findOne({
        studentNo: number
    }, (err, docu) => {
        if(err) {}
        else if(docu == null){
            res.send({
                pass: 0
            })
            return
        }else {
            group.find({}, (err, documents) => {
                var groups = [];
                for (var i = 0; i < documents.length; i++){
                    groups.push(JSON.parse(JSON.stringify(documents[i])))
                }

                for(var j = 0; j < groups.length; j++){
                    for (var k = 0; k < groups[j].members.length; k++){
                        if (group[j].members[k] == number){
                            res.send({
                                pass: 2
                            })
                            return
                        }
                    }
                }
                res.send({
                    pass: 1
                })
                return
            })
        }
    })
};

module.exports = {
    login: login
};
